﻿using System;
using System.Drawing;
using System.Windows.Forms;
using GuidanceExplorerAddIn.Properties;

namespace GuidanceExplorerAddIn.Controls
{
    class DatagridViewFilterHeaderCell : DataGridViewColumnHeaderCell
    {
        Point buttonLocation;
        Size buttonSize;
        Point _cellLocation = new Point();

        public event EventHandler OnFilterColumnClicked;

        public DatagridViewFilterHeaderCell()
        {
        }

        protected override void Paint(System.Drawing.Graphics graphics,
            System.Drawing.Rectangle clipBounds,
            System.Drawing.Rectangle cellBounds,
            int rowIndex,
            DataGridViewElementStates dataGridViewElementState,
            object value,
            object formattedValue,
            string errorText,
            DataGridViewCellStyle cellStyle,
            DataGridViewAdvancedBorderStyle advancedBorderStyle,
            DataGridViewPaintParts paintParts)
        {
            base.Paint(graphics, clipBounds, cellBounds, rowIndex,
            dataGridViewElementState, value,
            formattedValue, errorText, cellStyle,
            advancedBorderStyle, paintParts);
            
            Point p = new Point();
            Size s = new Size(cellBounds.Height - 8, cellBounds.Height - 8);
            p.X = cellBounds.Location.X + cellBounds.Width - s.Width * 2 - 8; // cellBounds.Location.X + (cellBounds.Width / 2) - (s.Width / 2);
            p.Y = cellBounds.Location.Y + (cellBounds.Height / 2) - (s.Height / 2);
            _cellLocation = cellBounds.Location;
            buttonLocation = p;
            buttonSize = s;

            CellBounds = cellBounds;

            Rectangle buttonBounds = new Rectangle(p,s);
            Rectangle imageBounds = buttonBounds;
            Bitmap bitmap = Resources.ColumnFilter;
            bitmap.MakeTransparent();
           // Image image = bitmap;
            graphics.DrawImage(bitmap, imageBounds);
//            ButtonRenderer.DrawButton(graphics, buttonBounds, image, imageBounds,false, System.Windows.Forms.VisualStyles.PushButtonState.Default);
        }
        protected override void OnMouseClick(DataGridViewCellMouseEventArgs e)
        {
                        
            if (OnFilterColumnClicked != null && ImageHit(e))
            {
                OnFilterColumnClicked(this,new EventArgs());
                //this.DataGridView.InvalidateCell(this);
            }             
            base.OnMouseClick(e);
        }

        /// <summary>
        /// Returns whether a click was done inside the header custom image bounds
        /// </summary>
        public bool ImageHit(DataGridViewCellMouseEventArgs e)
        { 
             Point p = new Point(e.X + _cellLocation.X, e.Y + _cellLocation.Y);
             return p.X >= buttonLocation.X && p.X <= buttonLocation.X + buttonSize.Width
                 && p.Y >= buttonLocation.Y && p.Y <= buttonLocation.Y + buttonSize.Height;
        }

        public Rectangle CellBounds { get; private set; }
    }
}
